<?php
/*
Name: Carbon Paper Style Archives
Version: 0.9
Author: Bertrand Scarpa
Description: Displays an archive of posts &amp; categories in the same style as http://www.carbonpaper.eu
*/

function carbonpaper_archives_posts($expanded_months = 3, $post_separator = "+")
{
	global $wpdb;
	setlocale(LC_ALL, 'fr_FR@euro');
	//echo strftime('%A %d %B, %Y',strtotime(get_the_time('m/d/Y'))); exit;
	$output = "";
	$postIndex = 1;

	$sql = "SELECT DATE_FORMAT(post_date, '%Y') AS Year, ";
	$sql .= "DATE_FORMAT(post_date, '%m') AS Month, ";
	$sql .= "COUNT(id) AS PostCount ";
	$sql .= "FROM $wpdb->posts ";
	$sql .= "WHERE DATE_FORMAT(post_date, '%Y') <> '0000'";
	$sql .= " AND post_status = 'publish'";
	$sql .= " AND post_type = 'post' ";
	$sql .= "GROUP BY DATE_FORMAT(post_date, '%M %Y'), DATE_FORMAT(post_date, '%Y') ";
	$sql .= "ORDER BY DATE_FORMAT(post_date, '%Y') DESC, DATE_FORMAT(post_date, '%m') DESC;";

	$months = $wpdb->get_results($sql);

	$sql = "SELECT wp.id, ";
	$sql .= "wp.post_title, ";
	$sql .= "DATE_FORMAT(wp.post_date, '%m') AS Month, ";
	$sql .= "DATE_FORMAT(wp.post_date, '%Y') AS Year, ";
	$sql .= "DATE_FORMAT(wp.post_date, '%M %Y') AS MonthYear, ";
	$sql .= "wp.post_date ";
	$sql .= "FROM $wpdb->posts wp ";
	$sql .= "WHERE wp.post_status = 'publish' ";
	$sql .= "AND wp.post_type = 'post' ";
	$sql .= "AND DATE_FORMAT(wp.post_date, '%Y') <> '0000' ";
	$sql .= "AND DATE_FORMAT(wp.post_date, '%Y-%m') > DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL $expanded_months MONTH), '%Y-%m') ";
	$sql .= "UNION ";
	$sql .= "SELECT '-1', ";
	$sql .= "'MONTH', ";
	$sql .= "DATE_FORMAT(wp.post_date, '%m') AS Month, ";
	$sql .= "DATE_FORMAT(wp.post_date, '%Y') AS Year, ";
	$sql .= "DATE_FORMAT(wp.post_date, '%M %Y') AS MonthYear, ";
	$sql .= "now() ";
	$sql .= "FROM $wpdb->posts wp ";
	$sql .= "WHERE wp.post_status = 'publish' ";
	$sql .= "AND wp.post_type = 'post' ";
	$sql .= "AND DATE_FORMAT(post_date, '%Y') <> '0000' ";
	$sql .= "AND DATE_FORMAT(wp.post_date, '%Y-%m') <= DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL $expanded_months MONTH), '%Y-%m') ";
	$sql .= "ORDER BY year DESC, month DESC, post_date DESC;";
	$posts = $wpdb->get_results($sql);

	// **** if posts exist : loop *********************************************************************************************************
	if (!empty($posts))
	{
		foreach ($posts as $post)
		{
			// **** if the current post is the first post or current year or current month have changed : open unit ***************
			if ($postIndex == 1)
			{
				$thisMonth = array_shift($months);
                                $post_count_label = $thisMonth->PostCount == 1 ? ' post':' posts';

                                $output .= '<div class="archive-unit">';
				$output .= '<h2>';
				$output .= '<a href="' . get_month_link($post->Year, $post->Month) . '"><span>' . utf8_encode(ucwords(strftime('%B %Y',mktime(0,0,0,$post->Month,1,$post->Year)))) . '</span> ';
				$output .= $thisMonth->PostCount . $post_count_label;
				$output .= "</a></h2>\n";

                                // **** if post_id <> -1 e.g. current month data are expanded *************************************************
				if ($post->id <> -1)
				{
        				$output .= "<p>";
        				$output .= '<a href="' . get_permalink($post->id) . '">' . $post->post_title . '</a>';
        			}
			}
                        // **** else if current post year and month are same than the previous record : detail with $post_separator ***********
                        else
                        {
                         $output .= '&nbsp;' . $post_separator . '&nbsp;<a href="' . get_permalink($post->id) . '">' . $post->post_title . '</a>';
			}

                        // **** if the current post is the last post or data are not expended ***
			if ($postIndex == $thisMonth->PostCount || $post->id == -1)
			{
                                // **** if post_id <> -1 : close expended current month data **************************************************
                                if ($post->id == -1)
                                {
                                        $output .= "</p>\n";
                                }

                                $output .= '</div><!-- /.archive-unit -->';
				$postIndex = 1;
			}
			else
                        {
                          $postIndex =  $postIndex + 1;
                        }
		}

	}
	// else : message ********************************************************************************************************************
	else
	{
		$output = "<p>None available</p>\n";
	}

	print $output;
}

function carbonpaper_archives_categories()
{
	global $wpdb;
	$output = "";

	$sql =  " SELECT t1.term_id, t2.term_id, name, description, count";
	$sql .= " FROM $wpdb->terms as t1, $wpdb->term_taxonomy as t2";
	$sql .= " WHERE t1.term_id = t2.term_id and taxonomy = 'category' and count > 0";
	$sql .= " ORDER BY name";

	$posts = $wpdb->get_results($sql);

	if (!empty($posts))
	{
		foreach ($posts as $post)
		{
			$post_count_label = $post->category_count == 1 ? ' post':' posts';

			$output .= '<div class="archive-unit">';
			$output .= '<h2>';
			$output .= '<a href="' . get_category_link($post->term_id) . '"><span>' . $post->name . '</span> ';
			$output .= $post->count . $post_count_label;
			$output .= "</a></h2>\n";

			if (strlen($post->description) > 0)
				$output .= "<p>" . $post->description . "</p>\n";

			$output .= '</div><!-- /.archive-unit -->';
		}
	}
	else
	{
		$output = "No categories defined";
	}

	print $output;
}
?>